<% load_ace_editor! %>

<% if @agent.errors.any? %>
  <div class="row well model-errors">
    <h2><%= pluralize(@agent.errors.count, "错误") %> 禁止此代理被保存:</h2>
    <% @agent.errors.full_messages.each do |msg| %>
      <p class='text-warning'><%= msg %></p>
    <% end %>
  </div>
<% end %>

<%= form_for(@agent,
             as: :agent,
             url: @agent.new_record? ? agents_path : agent_path(@agent),
             method: @agent.new_record? ? "POST" : "PUT",
             html: { class: 'agent-form' }) do |f| %>

  <%= hidden_field_tag :return, params[:return] %>

  <div class="row">
    <div class="col-md-6">
      <div class="row">

        <div class="col-md-12">
          <% if @agent.new_record? %>
            <div class="form-group type-select">
              <%= f.label :type,"类型" %>
              <%= f.select :type, options_for_select([['请选择一种代理类型', 'Agent', {title: ''}]] + Agent.types.map {|type| [type.name.gsub(/^.*::/, '').underscore.humanize.titleize, type, {title: h(Agent.build_for_type(type.name,current_user,{}).html_description.lines.first.strip)}] }, @agent.type), {}, :class => 'form-control' %>
            </div>
          <% end %>
        </div>

        <div class="agent-settings">
          <div class="col-md-12">
            <div class="form-group">
              <%= f.label :name,"代理名称" %>
              <%= f.text_field :name, :class => 'form-control' %>
            </div>

            <div class='oauthable-form'>
              <%= render partial: 'oauth_dropdown', locals: { agent: @agent } %>
            </div>

            <div class="form-group">
              <%= f.label :schedule, "时间表", :class => 'control-label' %>
              <div class="schedule-region" data-can-be-scheduled="<%= @agent.can_be_scheduled? %>">
                <div class="can-be-scheduled">
                  <%= f.select :schedule, options_for_select(Agent::SCHEDULES.map {|s| [s.humanize.titleize, s] }, @agent.schedule), {}, :class => 'form-control' %>
                </div>
                <span class='cannot-be-scheduled text-info'>无法安排此类代理.</span>
              </div>
            </div>

            <div class="controller-region" data-has-controllers="<%= !@agent.controllers.empty? %>">
              <div class="form-group">
                <%= f.label :controllers, "控制者" %>
                <span class="glyphicon glyphicon-question-sign hover-help" data-content="除了上面的系统定义的时间表之外，此代理可以由这些用户定义的代理运行或控制."></span>
                <div class="controller-list">
                  <%= agent_controllers(@agent) || '无' %>
                </div>
              </div>
            </div>

            <div class="control-link-region" data-can-control-other-agents="<%= @agent.can_control_other_agents? %>">
              <div class="can-control-other-agents">
                <div class="form-group">
                  <%= f.label :control_targets,"控制目标" %>
                  <%= f.select(:control_target_ids,
                               options_for_select(current_user.agents.map {|s| [s.name, s.id] },
                                                  @agent.control_target_ids),
                               {}, { multiple: true, size: 5, class: 'select2-linked-tags form-control', data: {url_prefix: '/agents'}}) %>
                </div>
              </div>
            </div>

            <div class='event-related-region' data-can-create-events="<%= @agent.can_create_events? %>">
              <div class="form-group">
                <%= f.label :keep_events_for, "事件持存" %>
                <span class="glyphicon glyphicon-question-sign hover-help" data-content="为了节省磁盘空间，您可以选择让此代理创建的事件在一段时间后过期。 确保将它们保持足够长的时间以允许任何后续代理使用它们."></span>
                <%= f.select :keep_events_for, options_for_select(Agent::EVENT_RETENTION_SCHEDULES, @agent.keep_events_for), {}, :class => 'form-control' %>
              </div>
            </div>

            <div class="form-group">
              <%= f.label :sources,"上级代理" %>
              <span class="glyphicon glyphicon-question-sign hover-help" data-content="此代理将从所选代理接收事件."></span>
              <div class="link-region" data-can-receive-events="<%= @agent.can_receive_events? %>">
                <% eventSources = (current_user.agents - [@agent]).find_all { |a| a.can_create_events? } %>
                <%= f.select(:source_ids,
                             options_for_select(eventSources.map {|s| [s.name, s.id] },
                                                @agent.source_ids),
                             {}, { :multiple => true, :size => 5, :class => 'select2-linked-tags form-control', data: {url_prefix: '/agents'} }) %>
                <span class='cannot-receive-events text-info'>此类代理无法接收事件.</span>
                <%= f.label :propagate_immediately,"立即执行", :class => 'propagate-immediately' do %>Propagate immediately
                  <%= f.check_box :propagate_immediately %>
                  &nbsp;
                  <span class="glyphicon glyphicon-question-sign hover-help" data-content="通常，Huginn每分钟将事件从源代理移动到接收代理一次。 选中此选项将导致立即接收此代理的源创建的事件。 这可以使用更多的CPU资源，但会减少事件创建和此代理接收之间的时间."></span>
                <% end %>
              </div>
            </div>

            <div class="form-group">
              <%= f.label :receivers,"下级代理" %>
              <span class="glyphicon glyphicon-question-sign hover-help" data-content="此代理创建的事件将发送到选定的代理."></span>
              <div class="event-related-region">
                <% eventTargets = (current_user.agents - [@agent]).find_all { |a| a.can_receive_events? } %>
                <%= f.select(:receiver_ids,
                             options_for_select(eventTargets.map {|s| [s.name, s.id] },
                                                @agent.receiver_ids),
                             {}, { :multiple => true, :size => 5, :class => 'select2-linked-tags form-control', data: {url_prefix: '/agents'} }) %>
                <span class='cannot-create-events text-info'>此类代理无法创建事件.</span>
              </div>
            </div>

            <% if current_user.scenario_count > 0 %>
              <div class="form-group">
                <%= f.label :scenarios,"所属场景" %>
                <span class="glyphicon glyphicon-question-sign hover-help" data-content="使用方案可以为组织分组代理集，并使它们易于导出和共享."></span>
                <%= f.select(:scenario_ids,
                             options_for_select(current_user.scenarios.pluck(:name, :id), @agent.scenario_ids),
                             {}, { :multiple => true, :size => 5, :class => 'select2-linked-tags form-control', data: {url_prefix: '/scenarios'} }) %>
              </div>
            <% end %>

          </div>

          <div class="col-md-12 agent-options">
            <%= render partial: 'options', locals: { agent: @agent } %>
          </div>
        </div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="row">
        <div class="col-md-12">
          <div class='well description'>
            <%= @agent.html_description unless @agent.new_record? %>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-md-12">
          <div class='well event-descriptions' style='display: none'></div>
        </div>
      </div>
    </div>
  </div>
<% end %>
